//
//  DeviceBrokenDownViewController.m
//  GetPowerDBO
//
//  Created by 周博 on 2017/6/22.
//  Copyright © 2017年 BogoZhou. All rights reserved.
//

#import "DeviceBrokenDownViewController.h"
#import "BGTextView.h"

@interface DeviceBrokenDownViewController ()<UITextViewDelegate>
{
    
}
@property (nonatomic,strong)  BGTextView*textView;

@end

@implementation DeviceBrokenDownViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self UISetting];
    [self creatTextView];
    [self addNoticeForKeyboard];
}

- (void)UISetting{
    _saveButton.layer.masksToBounds = YES;
    _saveButton.layer.cornerRadius = 5;
    
    _labelDeviceNum.text = _powerId;
    
}


- (void )creatTextView{
    
    _textView = [[BGTextView alloc] initWithFrame:CGRectMake(10, 110, kScreenSize.width - 20, 250-110)];
    _textView.textFont = [UIFont systemFontOfSize:15];
    _textView.placeholderStr = @"请输入文字";
    _textView.placeholderColor = kColorFrom0x(0xbebebe);
    _textView.textViewTextColor = [UIColor blackColor];
    _textView.backgroundColor = kColorFrom0x(0xf4f4f4);
    
    _textView.tintColor = [UIColor blackColor];
//    _textView.keyboardType = UIKeyboardTypeNumberPad;
    [_textView updateInfo];
    _textView.delegate = self;
    [_backView addSubview:_textView];
}


- (void)textViewDidChange:(UITextView *)textView{
    [_textView textViewDidChange:textView];
}

#pragma mark - 点击事件

- (IBAction)backButtonClick:(UIButton *)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (IBAction)saveButtonClick:(UIButton *)sender {

    [self brokenFun];
    
}


- (void)brokenFun{
    if ([BGFunctionHelper isNULLOfString:_textView.text]) {
        kAlert(@"请描述损坏详情");
        return;
    }
    [self.hub hideAnimated:YES];
    
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    // Set the label text.
    self.hub.label.text = NSLocalizedString(@"加载中...", @"HUD loading title");
    [[AFClient shareInstance] brokenPowerByEid:kUserId alias:_powerId content:_textView.text progressBlock:nil success:^(id responseBody) {
        [self.hub hideAnimated:YES];
        if ([responseBody[@"code"] integerValue] == 200) {
            [self alertVCByTitle:@"信息提交成功"];
        }
    } failure:^(NSError *error) {
        [self.hub hideAnimated:YES];
    }];
}


- (void)alertVCByTitle:(NSString *)title{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:title preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }];
    
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - 键盘通知
- (void)addNoticeForKeyboard {
    
    //注册键盘出现的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification object:nil];
    //注册键盘消失的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}

///键盘显示事件
- (void) keyboardWillShow:(NSNotification *)notification {
    //获取键盘高度，在不同设备上，以及中英文下是不同的
    CGFloat kbHeight = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    
    //计算出键盘顶端到inputTextView panel底端的距离(加上自定义的缓冲距离INTERVAL_KEYBOARD)
    CGFloat offset = (_textView.orginY + kbHeight) - (self.view.frame.size.height - kbHeight);
    
    // 取得键盘的动画时间，这样可以在视图上移的时候更连贯
    double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    //将视图上移计算好的偏移
    if(offset > 0) {
        [UIView animateWithDuration:duration animations:^{
            self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
        }];
    }
}

///键盘消失事件
- (void) keyboardWillHide:(NSNotification *)notify {
    // 键盘动画时间
    double duration = [[notify.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    //视图下沉恢复原状
    [UIView animateWithDuration:duration animations:^{
        self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
